import numpy as np
import torch
from d2l import torch as d2l

def  f(x):
    return x ** 2

def f_grad(x):
    return 2 * x

def gd(eta,f_grad):
    x = 10.0
    results = [x]
    for i in range(10):
        x -= eta * f_grad(x)
        results.append(float(x))

    print('epoch 10:{x:f}')
    return results

results = gd(0.2,f_grad)

def show_trace(results,f):
    n = max(abs(min(results)),abs(max(results)))
    f_line = torch.arange(-n,n,0.01)
    d2l.set_figsize()
    d2l.plot([f_line,results],[[f(x) for x in f_line],[f(x) for x in results]],'x','f(x)',fmts=['-','-o'])

show_trace(results,f)
d2l.plt.show()


